const { Display } = require('../../display/general')
const { getModelType } = require('../../util/utils')
const { ignoreIt, ignoreFunction } = require('../../util/ignore')

const models = {}

models.SingleEmissionModel = ignoreFunction

models.SingleEmmisionTowardsTargetModel = models.SingleEmissionModel

models.ArcEmissionModel = (e, p, w) => {
    if (e.angle) p.emission.angle = Display.angle(e.angle)
    if (e.offset) p.emission.offset = Display.angle(e.offset)
    if (e.count > 1) p.emission.count = e.count
}

models.EmissionWithOffsetsModel = (e, p, w) => {
    if (e.count > 1) p.emission.count = e.count
}

models.InstantDamageEmissionModel = ignoreFunction

models.RandomEmissionModel = (e, p, w) => {
    if (e.angle) p.emission.angle = Display.angle(e.angle)
    if (e.count > 1) p.emission.count = e.count
}

models.RandomTargetSpreadModel = (e, p, w) => {
    p.emission.spreadRadius = e.spread
}

models.RandomArcEmissionModel = (e, p, w) => {
    if (e.randomAngle) p.emission.angle = Display.angle(e.randomAngle)
    if (e.count > 1) p.emission.count = e.count
}

models.ParallelEmissionModel = (e, p, w) => {
    if (e.spreadLength) p.emission.spreadLength = e.spreadLength
    if (e.count > 1) p.emission.count = e.count
}

models.AdoraEmissionModel = (e, p, w) => {
    p.emission.angleBetweenProjectiles = Display.angle(e.angleBetween)
    if (e.count > 1) p.emission.count = e.count
}

models.GrappleEmissionModel = ignoreFunction

models.EmissionOverTimeModel = (e, p, w) => {
    p.emission.emissionInterval = Display.time(e.timeBetween)
    if (e.count > 1) p.emission.count = e.count
}

models.EmissionAtClosestPathSegmentModel = (e, p, w) => {
    p.emission.spacingBetween = e.spacing
    if (e.count > 1) p.emission.count = e.count
}

models.SendToBankModel = ignoreFunction

models.NecromancerEmissionModel = (e, p, w) => {
    //p.emission.minCount = e.minBloonsSpawnedPerWave
    //p.emission.maxCount = e.maxBloonsSpawnedPerWave
}

models.PrinceOfDarknessEmissionModel = (e, p, w) => {
    const { parseProjectile } = require('../projectile/parse')
    if (w) {
        w.projectile.push(parseProjectile(e.alternateProjectile))
    }
}

models.LineProjectileEmissionModel = (e, p, w) => {
    const { parseProjectile } = require('../projectile/parse')
    p.emission.emitFromTargetLength = e.emitFromTarget ? e.emitFromTargetLength : null
    if (w && e.projectileInitialHitModel) {
        w.projectile.push(parseProjectile(e.projectileInitialHitModel))
    }
    if (w && e.projectileAtEndModel) {
        w.projectile.push(parseProjectile(e.projectileAtEndModel))
    }
}

models.MultiEmissionModel = (e, p, w) => {
    const { parseProjectile } = require('../projectile/parse')
    w.projectile.push(parseProjectile(e.secondaryProjectileModel))
}

models.EmissionClosestOffsetModel = ignoreFunction

models.EmitFromSharedTowersModel = ignoreFunction

models.WererabbitEmissionModel = ignoreFunction

const parseEmission = (e, p, w) => {
    if (!e) return
    const type = getModelType(e)
    if (models[type]) {
        models[type](e, p, w)
    } else {
        p.emission = e
    }
    if (!e.behaviors || e.behaviors.length === 0) {
        return
    }
    e.behaviors.forEach(b => {
        const t = getModelType(b)
        if (ignoreIt(t)) { return }
        switch (t) {
            case 'AcidPoolEmissionModel': // only this needed to be parsed
                if (w) w.meta.acidPoolInterval = e.alwaysPoolOnCount
                break
            default:
                w.behaviors.push(b)
        }
    })
}

module.exports = {
    parseEmission
}